﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Data;

namespace System
{
    public static class DataHelper
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value">值</param>
        /// <returns>结果</returns>
        public static bool IsDBNull(object value)
        {
            bool result = false;

            if (!(Object.Equals(value, null)) && !(Object.Equals(value, System.DBNull.Value)))
            {
                if (int.Parse(value.ToString(), CultureInfo.InvariantCulture) != 0)
                {
                    result = true;
                }
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="result"></param>
        /// <returns>结果</returns>
        public static int ConvertResult(object value)
        {
            int num = 0;
            if (value != DBNull.Value)
            {
                num = Convert.ToInt32(value, CultureInfo.InvariantCulture);
            }
            return num;
        }

       
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbParameter"></param>
        /// <param name="dataType"></param>
        /// <param name="info"></param>
        public static void SetParameterDbType(DBParameter dbParameter, string dataType, ModifyInfo info)
        {
            Type dbType = null;

            if (info.NewValue != null)
            {
                dbType = info.NewValue.GetType();
            }
            if (dbType == null && info.OldValue != null)
            {
                dbType = info.OldValue.GetType();
            }

            if (dbType != null)
            {
                dataType = dataType.ToLower();

                if (dbType == typeof(byte[]))
                {
                    dbParameter.DbType = DbType.Binary;
                }
                if (dbType == typeof(decimal?))
                {
                    dbParameter.DbType = DbType.Decimal;
                }
                if (dataType == "money")
                {
                    dbParameter.DbType = DbType.Decimal;
                }
                if (dataType == "decimal")
                {
                    dbParameter.DbType = DbType.Decimal;
                }
                if (dataType == "bit")
                {
                    dbParameter.DbType = DbType.Boolean;
                }
                if (dataType == "date")
                {
                    dbParameter.DbType = DbType.Date;
                }
            }
        }
    }
}
